﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MechWars
{
    public class JMesh
    {
        public static Matrix staticViewMatrix;
        public static Matrix staticProjectionMatrix;
        public static GraphicsDevice staticGraphicsDevice;
        public BasicEffect effect;
        public List<VertexPositionNormalTexture> vertexes;
        public JMesh()
        {
            vertexes = new List<VertexPositionNormalTexture>();
            effect = new BasicEffect(staticGraphicsDevice);
            effect.Projection = staticProjectionMatrix;
            effect.View = staticViewMatrix;
            effect.World = Matrix.Identity;
            effect.EnableDefaultLighting();
            effect.LightingEnabled = true;
        }
        public void Update()
        {
            effect.Projection = staticProjectionMatrix;
            effect.View = staticViewMatrix;
        }
        public void AddTriangle(Vector3 a, Vector3 b, Vector3 c)
        {
            Vector3 cross = Vector3.Cross(b - a, c - a);
            vertexes.Add(new VertexPositionNormalTexture(a, cross, new Vector2(0, 0)));
            vertexes.Add(new VertexPositionNormalTexture(c, cross, new Vector2(1, 0)));
            vertexes.Add(new VertexPositionNormalTexture(b, cross, new Vector2(0, 1)));
        }
        public void AddQuad(Vector3 a,Vector3 b,Vector3 c,Vector3 d)
        {
            AddTriangle(a, b, c);
            AddTriangle(a, c, d);
        }
        public void AddCubeBoid(Vector3 a, Vector3 b)
        {
            Vector3[] p = 
            {
                a,
                new Vector3(b.X,a.Y,a.Z),
                new Vector3(b.X,a.Y,b.Z),
                new Vector3(a.X,a.Y,b.Z),

                new Vector3(a.X,b.Y,a.Z),
                new Vector3(b.X,b.Y,a.Z),
                b,
                new Vector3(a.X,b.Y,b.Z)
            };
            //top and bottom
            AddQuad(p[3], p[2], p[1], p[0]);
            AddQuad(p[5], p[6], p[7], p[4]);
            
            //back and frount
            AddQuad(p[5], p[4], p[0], p[1]);
            AddQuad(p[7], p[6], p[2], p[3]);
            //left and right
            AddQuad(p[6], p[5], p[1], p[2]);
            AddQuad(p[4], p[7], p[3], p[0]);
            
            //top and bottom
            /*AddQuad(p[0], p[1], p[2], p[3]);
            AddQuad(p[4], p[7], p[6], p[5]);

            //back and frount
            AddQuad(p[1], p[0], p[4], p[5]);
            AddQuad(p[3], p[2], p[6], p[7]);
            //left and right
            AddQuad(p[2], p[1], p[5], p[6]);
            AddQuad(p[0], p[3], p[7], p[4]);*/
        }
        public void RemoveTriangle()
        {
            if (vertexes.Count >= 3)
            {
                vertexes.RemoveAt(vertexes.Count - 1);
                vertexes.RemoveAt(vertexes.Count - 1);
                vertexes.RemoveAt(vertexes.Count - 1);
            }
        }
        public void draw()
        {
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                staticGraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertexes.ToArray(), 0, vertexes.Count / 3, VertexPositionNormalTexture.VertexDeclaration);
            }
        }
        public void draw(BasicEffect overideEffect)
        {
            foreach (EffectPass pass in overideEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                staticGraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertexes.ToArray(), 0, vertexes.Count / 3, VertexPositionNormalTexture.VertexDeclaration);
            }
        }

    }
}
